package com.dz.config;

import com.dz.constant.RabbitMQConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;

/**
 * @author 鄧浩奇
 * @classname RabbitMQConfiguration
 * @description
 * @since 2024/3/28 16:52
 **/

@Slf4j
//@Configuration
public class RabbitMQConfiguration {

    @Value("${spring.rabbitmq.host}")
    private String host;

    @Value("${spring.rabbitmq.port}")
    private int port;

    @Value("${spring.rabbitmq.username}")
    private String username;

    @Value("${spring.rabbitmq.password}")
    private String password;

    @Value("${spring.rabbitmq.virtual-host}")
    private String virtualhost;

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host,port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualhost);
//        connectionFactory.setPublisherConfirms(true);
        return connectionFactory;
    }

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    //必须是prototype类型
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                if (ack) {
                    log.info("消息发送成功：{}",correlationData);
                } else log.error("消息发送失败：{}",cause);
            }
        });
        return rabbitTemplate;
    }


    @Bean("LongQueue")
    public Queue skyLongQueue() {
        return new Queue(RabbitMQConstant.SKY_Long_QUEUE,false);
    }

    @Bean("ListQueue")
    public Queue skyListQueue() {
        return new Queue(RabbitMQConstant.SKY_List_QUEUE,false);
    }

    @Bean
    DirectExchange exchange() {
        return new DirectExchange(RabbitMQConstant.SKY_EXCHANGE);
    }

    @Bean
    Binding longBinding(@Qualifier("LongQueue") Queue longQueue, DirectExchange dishExchange) {
        return BindingBuilder.bind(longQueue).to(dishExchange).with(RabbitMQConstant.SKY_Long_ROUTING_KEY);
    }

    @Bean
    Binding listBinding(@Qualifier("ListQueue") Queue listQueue, DirectExchange exchange) {
        return BindingBuilder.bind(listQueue).to(exchange).with(RabbitMQConstant.SKY_List_ROUTING_KEY);
    }

    // 如果有其他交换机、队列或绑定，可以在这里继续添加....
}
